package com.sz.app.edi.service.order.impl;

import com.sz.app.edi.rest.edi.order.dto.AddressMode;
import com.sz.app.edi.rest.edi.order.dto.Cargo;
import com.sz.app.edi.rest.edi.order.dto.CusOrderDto;
import com.sz.app.edi.rest.edi.order.dto.Optional;
import com.sz.app.edi.service.order.CusOrderService;
import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.logistics.ord.dto.OrdOrderAdditionalDto;
import com.sz.biz.logistics.ord.dto.OrdOrderCargoDto;
import com.sz.biz.logistics.ord.dto.OrdOrderDto;
import com.sz.biz.logistics.ord.dto.OrderAddressModel;
import com.sz.biz.logistics.ord.entity.OrderStatus;
import com.sz.biz.logistics.ord.service.OrdOrderCommService;
import com.sz.biz.logistics.ord.service.OrdOrderService;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.ObjectUtils;
import org.apache.poi.util.XMLHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;


/**
 * Function: edi订单service
 * Author: hpeng.wang.<br>
 * Date: 2017-06-23 16:08:00.
 */
@WebService(serviceName = "CusOrderService", // 与接口中指定的name一致
        targetNamespace = "http://order.ord.service.edi.app.logistics.hna.com/", // 与接口中的命名空间一致,一般是接口的包名倒
        endpointInterface = "com.sz.app.edi.service.order.CusOrderService"// 接口地址
)
@Service
public class CusOrderServiceImpl implements CusOrderService {
    public static final String NCID = "NCID";
    @Autowired
    private OrdOrderService ordOrderService;
    @Autowired
    private OrdOrderCommService orderCommService;

    @Override
    public String saveOrder(String str) {

        if (str.contains(NCID)) {
            String ss = (String) XMLHelper.getDocumentBuilderFactory().getAttribute("ncid");
            //  ordOrderService.find
        }
        //String orderNo = ordOrderService.saveOrderDto(ordOrderDto);
        return "";
    }

    @Override
    public String saveRestOrder(CusOrderDto cusOrderDto) {
        OrdOrderDto ordOrderDto = new OrdOrderDto();
        AddressMode addressMode;

        //填充发件人地址
        OrderAddressModel fromAddress = new OrderAddressModel();
        addressMode = cusOrderDto.getFromAddress();
        EntityUtils.copyPropertiesIgnoreNull(addressMode, fromAddress);
        ordOrderDto.setFromAddress(fromAddress);
        ordOrderDto.setFromName(addressMode.getName());
        ordOrderDto.setFromPhone(addressMode.getPhone());
        ordOrderDto.setFromTelephone(addressMode.getTelephone());
        boolean bOk = orderCommService.checkAddress(ordOrderDto.getFromAddress());
        if (!bOk) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "fromAddress");
        }

        //填充收件人地址
        OrderAddressModel toAddress = new OrderAddressModel();
        addressMode = cusOrderDto.getToAddress();
        EntityUtils.copyPropertiesIgnoreNull(addressMode, toAddress);
        ordOrderDto.setToAddress(toAddress);
        ordOrderDto.setToName(addressMode.getName());
        ordOrderDto.setToPhone(addressMode.getPhone());
        ordOrderDto.setToTelephone(addressMode.getTelephone());
        bOk = orderCommService.checkAddress(ordOrderDto.getToAddress());

        if (!bOk) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "toAddress");
        }

        //填充揽件人地址
        addressMode = cusOrderDto.getPickUpAddress();
        if (ObjectUtils.isNotEmpty(addressMode)) {
            OrderAddressModel pickupAddress = new OrderAddressModel();
            EntityUtils.copyPropertiesIgnoreNull(addressMode, pickupAddress);
            ordOrderDto.setFetchAddress(pickupAddress);
            ordOrderDto.setFetchName(addressMode.getName());
            ordOrderDto.setFetchPhone(addressMode.getPhone());
            ordOrderDto.setFetchTelephone(addressMode.getTelephone());
            bOk = orderCommService.checkAddress(ordOrderDto.getFetchAddress());
            if (!bOk) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL, "pickUpAddress");
            }
        }

        ordOrderDto.setOrderStatus(OrderStatus.COMMITED);
        ordOrderDto.setCargoType(cusOrderDto.getCargoType());
        ordOrderDto.setCusUserId(cusOrderDto.getCusUserId());
        ordOrderDto.setCustomerId(cusOrderDto.getCustomerId());
        ordOrderDto.setExternalNo(cusOrderDto.getExternalNo());
        ordOrderDto.setProductUid(cusOrderDto.getProduct().getProductUid());
        ordOrderDto.setCusWeight(cusOrderDto.getCusWeight());
        ordOrderDto.setType(cusOrderDto.getType());
        ordOrderDto.setPackageNum(cusOrderDto.getPackageNum());
        ordOrderDto.setCusWeightUnit("kg");


        //处理货物明细
        List<Cargo> cargoList = cusOrderDto.getCargoList();
        List<OrdOrderCargoDto> cargoDtoList = new ArrayList<>();
        for (Cargo cargo : cargoList) {
            OrdOrderCargoDto cargoDto = new OrdOrderCargoDto();
            cargoDto.setGoodsNameCn(cargo.getNameCn());
            cargoDto.setGoodsNameEn(cargo.getNameEn());
            cargoDto.setHsCode(cargo.getHsCode());
            cargoDto.setGoodsNumber(cargo.getNumber());
            cargoDto.setGoodsPrice(cargo.getPrice());
            cargoDto.setGoodsUnit(cargo.getGoodsUnit());
            cargoDtoList.add(cargoDto);
        }
        ordOrderDto.setOrderCargoDtos(cargoDtoList);


        //处理可选服务
        List<OrdOrderAdditionalDto> orderAdditionalDtos = new ArrayList<>();
        List<Optional> optionalList = cusOrderDto.getProduct().getOptional();
        for (Optional optional : optionalList) {
            OrdOrderAdditionalDto additionalDto = new OrdOrderAdditionalDto();
            additionalDto.setServiceTypeCode(optional.getServiceTypeCode());
            orderAdditionalDtos.add(additionalDto);
        }
        ordOrderDto.setOrderAdditionalDtos(orderAdditionalDtos);


        String waybillNo = ordOrderService.saveOrderDto(ordOrderDto);
        return waybillNo;
    }
}